/**
 * Copyright:Copyright(c) 2016
 * Company:上海悠活网络科技有限公司
 */
package cn.dakaqi.services.impl;

import cn.dakaqi.dao.PassportSeqDao;
import cn.dakaqi.entities.PassportSeq;
import cn.dakaqi.services.PassportSeqService;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * 类名称：PassportSeqServiceImpl <br>
 * 类描述：<br>
 *
 * @author: chunyang.Zhang
 * @since: 2016/12/20 12:15
 * @version: 1.0.0
 */
@Transactional
@Component(value = "passportSeqService")
public class PassportSeqServiceImpl implements PassportSeqService
{
    @Autowired
    PassportSeqDao passportSeqDao;

    @Override
    public PassportSeq findByName(String name)
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("name", new SearchFilter("name", SearchFilter.Operator.EQ, name));
        Specification<PassportSeq> spec = DynamicSpecifications.bySearchFilter(filters.values(), PassportSeq.class);
        List<PassportSeq> list = this.passportSeqDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(0);
        return null;
    }

    @Override
    public void updatePassportSeq(Long id)
    {
        PassportSeq passportSeq = passportSeqDao.findOne(id);
        if(null != passportSeq)
        {
            passportSeq.setSeq(passportSeq.getSeq()+1);
            this.passportSeqDao.save(passportSeq);
        }
    }
    @Override
    public int updatePassportSeqByName(String name)
    {
        int value = 0;
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("name", new SearchFilter("name", SearchFilter.Operator.EQ, name));
        Specification<PassportSeq> spec = DynamicSpecifications.bySearchFilter(filters.values(), PassportSeq.class);
        List<PassportSeq> list = this.passportSeqDao.findAll(spec);
        if(null != list && list.size()>0)
        {
            PassportSeq passportSeq = list.get(0);
            if(null != passportSeq)
            {
                value = passportSeq.getSeq();
                passportSeq.setSeq(passportSeq.getSeq()+1);
                this.passportSeqDao.save(passportSeq);
            }
        }
        return value;
    }
}
 
 